package org.gc.baseframe.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import javax.swing.Icon;
import javax.swing.ImageIcon;

/**
 * <p>Title: </p>
 * <p>Description: Resource Loader</p>
 * <p>Copyright: Copyright (c) 2009</p>
 * <p>Company: </p>
 * <p>LastModified: </p>
 * @author wangdong
 * @version 1.0
 */
public class ResourseLoader {

    public ResourseLoader() {
    }

    /**
     * Get icon from jar
     * @param path String
     * @param cls Class
     * @return Icon
     */
    public static Icon getIconFromJar(String path, Class cls) {
        Icon image = null;
        InputStream is = cls.getResourceAsStream(path);
        if(is != null) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                byte abyte0[] = new byte[1024];
                for(int i = 0; (i = is.read(abyte0)) >= 0; ) {
                    baos.write(abyte0, 0, i);
                }
//        return new IconUIResource(new ImageIcon(baos.
//                                                toByteArray()));
                return new ImageIcon(baos.toByteArray()); //ImageIcon implements Icon
            } catch(IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return image;
    }

    /**
     * Get text from jar
     * @param path String
     * @param charset String
     * @param cls Class
     * @return String
     */
    public static String getTextFromJar(String path, String charset, Class cls) {
        StringBuffer s1 = new StringBuffer();
        InputStream is = cls.getResourceAsStream(path);
        if(is != null) {
            BufferedReader br = null;
            try {
                br = new BufferedReader(new InputStreamReader(
                        is, charset));
            } catch(UnsupportedEncodingException ex) {
                br = new BufferedReader(new InputStreamReader(
                        is));
            }
            String s2;
            try {
                while((s2 = br.readLine()) != null) {
                    s1.append(s2).append('\n');
                }
            } catch(IOException e){
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return s1.toString();
    }

    /**
     * Get file from jar
     * @param path String
     * @param cls Class
     * @return File
     */
    public static File getFileFromJar(String path, Class cls) {
        int index = path.indexOf('.');
        String postFix = path.substring(index); //(.xml or .txt)
        String dir = System.getProperty("java.io.tmpdir");
        int rand = (int)(Math.random() * 10000);
        int rand1 = (int)(Math.random() * 100);
        String name = String.valueOf(System.currentTimeMillis() +
                                     "tmp" + String.valueOf(rand1) +
                                     String.valueOf(rand)) + postFix; //temp file
        File file = new File(dir, name);
        InputStream is = cls.getResourceAsStream(path);
        BufferedInputStream bis = null;
        FileOutputStream out = null;
        BufferedOutputStream bos = null;
        if(is != null) {
            try {
                bis = new BufferedInputStream(is);
                out = new FileOutputStream(file);
                bos = new BufferedOutputStream(out);
                int t;
                while((t = bis.read()) != -1) {
                    bos.write(t);
                }
                bos.flush();

            } catch(IOException ex){
                ex.printStackTrace();
            } finally {
                try {
                    bos.close();
                    out.close();
                    bis.close();
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return file;
    }

}
